//
//  NavBarView.m
//  ZhiHuDaily
//
//  Created by albert on 16/8/17.
//  Copyright © 2016年 albert. All rights reserved.
//

#import "NavBarView.h"

@interface NavBarView ()
/** 标题label */
@property (nonatomic, weak) UILabel *titleLabel;

/** 标题默认center */
@property (nonatomic, assign) CGPoint defaltCenter;

/** 默认返回按钮 */
@property (nonatomic, weak) UIButton *backButton;
@end

@implementation NavBarView

/** 标题label默认宽 */
static CGFloat defaltW = 260;
/** 标题view默认宽高 */
static CGFloat defaltWH = 30;

/** 自定义构造方法 */
- (instancetype)initWithTitle:(NSString *)title showBackTitle:(BOOL)showBackTitle{
    if (self = [super init]) {
        self.titleLabel.text = title;
        if (showBackTitle == NO) {
            [self deleteBackButtonTitle];
        }
    }
    return self;
}

/** 类方法 初始化 */
+ (instancetype)barViewWithTitle:(NSString *)title showBackTitle:(BOOL)showBackTitle{
    NavBarView *barView = [[NavBarView alloc] initWithTitle:title showBackTitle:showBackTitle];
    return barView;
}

/** 类方法 初始化 */
+ (instancetype)barView{
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)setup{
    self.frame = CGRectMake(0, 0, JKScreenW, JKNavBarHeight);
    
    // 标题label
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    // 默认返回按钮
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [backButton setImage:[UIImage imageNamed:@"Back_White"] forState:(UIControlStateNormal)];
    [backButton setImage:[UIImage imageNamed:@"Back"] forState:(UIControlStateHighlighted)];
    [backButton setTitle:@"返回" forState:(UIControlStateNormal)];
    
    [backButton setTitleColor:[UIColor colorWithRed:0/255.0 green:152/255.0 blue:234/255.0 alpha:1] forState:(UIControlStateHighlighted)];
    
    [backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //让button里面的内容移到最左边
    backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    backButton.titleEdgeInsets = UIEdgeInsetsMake(0, -18, 0, 0);
    //设置button的内容偏移，可以与上面的Alignment配合，也可以直接设置偏移量更大些，比如-10改为-80
    //backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);//想让导航条的按钮距离边上更近些，可以用这个
    
    [self addSubview:backButton];
    self.backButton = backButton;
    
    self.defaltCenter = CGPointMake(self.centerX, self.centerY + 12);
}

// 布局子控件
- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.titleLabel.width = defaltW;
    self.titleLabel.height = defaltWH;
    self.titleLabel.center = self.defaltCenter;
    
    self.backButton.frame = CGRectMake(5, JKStatusBarHeight + 2, 80, JKNavBarHeight - JKStatusBarHeight);
    _leftButtonFrame = self.backButton.frame;
    _rightButtonFrame = CGRectMake(self.width - (JKNavBarHeight - JKStatusBarHeight), JKStatusBarHeight, JKNavBarHeight - JKStatusBarHeight, JKNavBarHeight - JKStatusBarHeight);
}

- (void)backButtonClick:(UIButton *)button{
    !self.backBtnClickBlock ? : self.backBtnClickBlock(button);
}

- (void)deleteBackButtonTitle{
    [self.backButton setTitle:nil forState:(UIControlStateNormal)];
    [self.backButton setTitle:nil forState:(UIControlStateHighlighted)];
}

- (void)setBgAlpha:(CGFloat)bgAlpha{
    _bgAlpha = bgAlpha;
    
    self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:bgAlpha];
}

- (void)setTitleView:(UIView *)titleView{
    _titleView = titleView;
    
    [self addSubview:titleView];
    
    if (titleView.width <= 0) {
        titleView.width = defaltWH;
        titleView.height = defaltWH;
    }

    titleView.center = self.defaltCenter;
    
    [self.titleLabel removeFromSuperview];
}

- (void)setTitle:(NSString *)title{
    _title = [title copy];
    self.titleLabel.text = self.title;
}

- (void)setLeftButton:(UIButton *)leftButton{
    _leftButton = leftButton;
    
    [self addSubview:leftButton];
    
    if (leftButton.width <= 0) {
        leftButton.frame = CGRectMake(10, JKStatusBarHeight, JKNavBarHeight - JKStatusBarHeight, JKNavBarHeight - JKStatusBarHeight);
    }
    
    [self.backButton removeFromSuperview];
}

- (void)setRightButton:(UIButton *)rightButton{
    _rightButton = rightButton;
    
    [self addSubview:rightButton];
    
    if (rightButton.width <= 0) {
        rightButton.frame = CGRectMake(self.width - (JKNavBarHeight - JKStatusBarHeight), JKStatusBarHeight, JKNavBarHeight - JKStatusBarHeight, JKNavBarHeight - JKStatusBarHeight);
    }
}

- (void)setBackButtonTitle:(NSString *)backButtonTitle{
    _backButtonTitle = [backButtonTitle copy];
    
    [self.backButton setTitle:backButtonTitle forState:(UIControlStateNormal)];
}

- (void)setLeftButtonHidden:(BOOL)leftButtonHidden{
    _leftButtonHidden = leftButtonHidden;
    self.backButton.hidden = leftButtonHidden;
    self.leftButton.hidden = leftButtonHidden;
}

- (void)setRightButtonHidden:(BOOL)rightButtonHidden{
    _rightButtonHidden = rightButtonHidden;
    self.rightButton.hidden = rightButtonHidden;
}

#pragma mark - 更新皮肤
- (void)changeToNightSkinWithBgAlpha:(CGFloat)bgAlpha{
    self.backgroundColor = [JKColor(60, 60, 60) colorWithAlphaComponent:bgAlpha];
    self.titleLabel.textColor = JKColor(186, 186, 186);
    [self.backButton setTitleColor:self.titleLabel.textColor forState:(UIControlStateNormal)];
    [self.leftButton setTitleColor:self.titleLabel.textColor forState:(UIControlStateNormal)];
    [self.rightButton setTitleColor:self.titleLabel.textColor forState:(UIControlStateNormal)];
}

- (void)changeToDaySkinWithBgAlpha:(CGFloat)bgAlpha{
    self.backgroundColor = [JKMainColor colorWithAlphaComponent:bgAlpha];
    self.titleLabel.textColor = [UIColor  whiteColor];
    [self.backButton setTitleColor:self.titleLabel.textColor forState:(UIControlStateNormal)];
    [self.leftButton setTitleColor:self.titleLabel.textColor forState:(UIControlStateNormal)];
    [self.rightButton setTitleColor:self.titleLabel.textColor forState:(UIControlStateNormal)];
}

- (void)changeToNightSkin{ // 夜间
    [self changeToNightSkinWithBgAlpha:1];
}

- (void)changeToDaySkin{ // 白天
    [self changeToDaySkinWithBgAlpha:1];
}
@end
